package com.beyond.trace.controller.storage;

import com.beyond.trace.domain.TraceBase;
import com.beyond.trace.domain.storage.Order;
import com.beyond.trace.domain.storage.OrderVo;
import com.beyond.trace.requestVo.OrderQuery;
import com.beyond.trace.requestVo.StoreVo;
import com.beyond.trace.service.ITraceBaseService;
import com.beyond.trace.service.storage.IOrderService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 销售订单信息
 * 
 * @author yao
 * @date 2022-03-02
 */
@RestController
@RequestMapping("/order")
@Api(value = "销售订单信息", tags = "销售订单信息")
public class OrderController extends BaseController
{
    @Autowired
    private IOrderService orderService;

    @GetMapping("/getOrderPage")
    @ApiOperation("分页查询订单信息")
    public TableDataInfo getOrderPage(OrderQuery orderQuery)
    {
        startPage();
        List<Order> list = orderService.getOrderPage(orderQuery);
        return getDataTable(list);
    }

    @GetMapping(value = "/getOrderById")
    @ApiOperation("通过id查询订单信息")
    public AjaxResult getOrderById(String id)
    {
        if(StringUtils.isEmpty(id)) {
            return AjaxResult.error("订单id不能为空");
        }
        if(orderService.getById(id) == null) {
            return AjaxResult.error("没有查到订单信息");
        }
        return AjaxResult.success(orderService.getOrderById(id));
    }

    @PostMapping(value = "/addOrder")
    @ApiOperation("添加订单信息")
    public AjaxResult addOrder(@RequestBody @Validated OrderVo orderVo)
    {
        return AjaxResult.success(orderService.addOrder(orderVo));
    }

    @PostMapping(value = "/editOrder")
    @ApiOperation("修改订单信息")
    public AjaxResult editOrder(@RequestBody OrderVo orderVo)
    {
        if(StringUtils.isEmpty(orderVo.getId())) {
            return AjaxResult.error("订单id不能为空");
        }
        return AjaxResult.success(orderService.editOrder(orderVo));
    }

    @GetMapping(value = "/deleteOrderById")
    @ApiOperation("删除订单信息")
    public AjaxResult deleteOrder(String id)
    {
        if(StringUtils.isEmpty(id)) {
            return AjaxResult.error("订单id不能为空");
        }
        return AjaxResult.success(orderService.deleteOrderById(id));
    }

    @GetMapping(value = "/getStorePage")
    @ApiOperation("获取产品的库存")
    public TableDataInfo getStorePage(@RequestParam(name = "productName",required = false) String productName)
    {
        startPage();
        List<StoreVo> storePage = orderService.getStorePage(productName);
        if(StringUtils.isNotEmpty(storePage)) {
            storePage.forEach(s->{
                s.setStoreCount(s.getInCount()-s.getOutCount());
            });
        }
        return getDataTable(storePage);
    }



}
